package com.sdp.right.control;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.right.cache.clean.RightCleanCache;
import com.sdp.right.db.sys_org.service.ISysOrgService;
import com.sdp.right.entity.SysOrg;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2023-10-17
 */
@RestController
@RequestMapping("/SysOrgController")
public class SysOrgController {
	
	@Autowired
	private ISysOrgService iSysOrgService;
	
	@Autowired
	private RightCleanCache	rightCleanCache;
	
	
	@PostMapping(value = "/queryOrgTableTree")
	public Result<?> queryOrgTableTree(@RequestBody DataCenter dataCenter) {
		return Result.ok(iSysOrgService.queryOrgTableTree());
	}
	
	@PostMapping(value = "/queryOrgTreeSelect")
	public Result<?> queryOrgTreeSelect(){
		return Result.ok(iSysOrgService.queryOrgTreeSelect());
	}
	
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody SysOrg sysOrg) {
		if(sysOrg==null) {
			return Result.error("组织信息为空");
		}
		if(StringUtils.isEmpty(sysOrg.getKeyId())) {
			SysOrg org = iSysOrgService.querySysOrgByNo(sysOrg.getOrgNo());
			if(org!=null) {
				return Result.error("此组织编号的组织信息已存在");
			}
			Result<?> r = setOrgLevel(sysOrg);
			if(r.isSuccess()) {
				iSysOrgService.save(sysOrg);
			}else {
				return r;
			}
			
		}else {
			SysOrg org = iSysOrgService.querySysOrgByNo(sysOrg.getOrgNo());
			if(org!=null && !org.getKeyId().equals(sysOrg.getKeyId())) {
				return Result.error("此组织编号的组织信息已存在");
			}
			Result<?> r = setOrgLevel(sysOrg);
			if(r.isSuccess()) {
				iSysOrgService.updateById(sysOrg);
			}else {
				return r;
			}
			
		}
		rightCleanCache.cleanOrgCache();
		return Result.ok();
	}
	
	
	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="orgNo",required=true) String orgNo) {
		if(StringUtils.isEmpty(orgNo)) {
			return Result.error("需要删除的记录的信息不完整");
		}
		//删除自己以及所有子节点
		iSysOrgService.removeSysOrgByNo(orgNo);
		//删除缓存
		rightCleanCache.cleanOrgCache();
		return Result.ok();
	}
	
	/**
	 * 设置层级
	 * @param sysOrg
	 * @return
	 */
	private Result<?> setOrgLevel(SysOrg sysOrg) {
		if(StringUtils.isEmpty(sysOrg.getParentNo())) {
			sysOrg.setOrgLevel("["+sysOrg.getOrgNo()+"]");
		}else if(sysOrg.getParentNo().equals(sysOrg.getOrgNo())){
			return Result.error("选择的父组织不能为自己");
		}else{
			SysOrg org = iSysOrgService.querySysOrgByNo(sysOrg.getParentNo());
			if(org==null) {
				return Result.error("选择的父组织已经不存在");
			}else if(org.getOrgLevel().indexOf("["+sysOrg.getOrgNo()+"]")>-1) {
				return Result.error("选择的父组织不符合要求，存在相互依赖关系");
			}
			sysOrg.setOrgLevel(org.getOrgLevel()+"["+sysOrg.getOrgNo()+"]");
			
		}
		return Result.ok();
	}

}

